﻿<UserControl x:Class="ScrumFactory.Artifacts.ArtifactsList"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             
             xmlns:props="clr-namespace:ScrumFactory.Artifacts.Properties"
             xmlns:SF_Comp="clr-namespace:ScrumFactory.Composition;assembly=ScrumFactory.Composition"                             
             xmlns:SF_Ext="clr-namespace:ScrumFactory.Windows.Helpers.Extensions;assembly=ScrumFactory.Windows.Helpers"
             
             xmlns:local="clr-namespace:ScrumFactory.Artifacts"
             
             x:Name="thisView"
             
             
             
             d:DesignHeight="300" d:DesignWidth="300">
    
    <UserControl.Resources>
        
        <local:ArtifactImageConverter x:Key="ImageConverter" />
        <local:CanEnlargeConverter x:Key="CanEnlargeConverter" />

        <Storyboard x:Key="expandStoryboard">
            <DoubleAnimation Storyboard.TargetName="image" Storyboard.TargetProperty="RenderTransform.ScaleX" To="3" Duration="0:0:0.2" />
            <DoubleAnimation Storyboard.TargetName="image" Storyboard.TargetProperty="RenderTransform.ScaleY" To="3" Duration="0:0:0.2" />                        
        </Storyboard>
        
        <Storyboard x:Key="shrinkStoryboard">
            <DoubleAnimation Storyboard.TargetName="image" Storyboard.TargetProperty="RenderTransform.ScaleX" To="1" Duration="0:0:0.2" />
            <DoubleAnimation Storyboard.TargetName="image" Storyboard.TargetProperty="RenderTransform.ScaleY" To="1" Duration="0:0:0.2" />
        </Storyboard>

        <Style x:Key="ArtifactImageButton" TargetType ="{x:Type Button}">
            <Setter Property="Command" Value="{Binding Model.GoToLinkCommand, ElementName=thisView}"/>
            <Setter Property="CommandParameter" Value="{Binding ArtifactPath}"/>            
            <Setter Property="Cursor" Value="Hand"/>            
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">

                        <Grid 
                            Background="#DDDDDD" Margin="0,0,10,5" HorizontalAlignment="Stretch" Width="100" Height="100">

                            <Image x:Name="image" VerticalAlignment="Top"                                   
                                   Source="{TemplateBinding Content, Converter={StaticResource ImageConverter}}" Stretch="Uniform" Width="100" MaxHeight="100">                                
                            </Image>

                            <TextBlock 
                                x:Name="text" Padding="3" Margin="0,0,0,0" MaxHeight="40" Foreground="White"
                                Text="{TemplateBinding Tag}" VerticalAlignment="Bottom"  ToolTip="{TemplateBinding Tag}"                                  
                                FontSize="{StaticResource SmallFontSize}" TextWrapping="Wrap" >
                                <TextBlock.Background>
                                    <SolidColorBrush Color="Gray" Opacity="0.8"/>
                                </TextBlock.Background>
                            </TextBlock>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>   
            <Style.Triggers>
                <DataTrigger Binding="{Binding ., Converter={StaticResource CanEnlargeConverter}}" Value="True">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="ArtifactEnlargeImageButton" TargetType ="{x:Type Button}">
            <Setter Property="Command" Value="{Binding Model.GoToLinkCommand, ElementName=thisView}"/>
            <Setter Property="CommandParameter" Value="{Binding ArtifactPath}"/>
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">

                        <Grid Background="#DDDDDD" Margin="0,0,10,5" >

                            <Image x:Name="image" VerticalAlignment="Top"
                                   RenderTransformOrigin="1,1" 
                                   Source="{TemplateBinding Content, Converter={StaticResource ImageConverter}}" Stretch="Uniform" MaxHeight="100" Width="100">
                                <Image.RenderTransform>
                                    <ScaleTransform ScaleX="1" ScaleY="1"/>
                                </Image.RenderTransform>
                            </Image>

                            <TextBlock 
                                x:Name="text" Padding="3" Margin="0,0,0,0" MaxHeight="40" Foreground="White" Width="100"
                                Text="{TemplateBinding Tag}" VerticalAlignment="Bottom"  ToolTip="{TemplateBinding Tag}"                                  
                                FontSize="{StaticResource SmallFontSize}" TextWrapping="Wrap" >
                                <TextBlock.Background>
                                    <SolidColorBrush Color="Gray" Opacity="0.8"/>
                                </TextBlock.Background>
                            </TextBlock>
                        </Grid>

                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Panel.ZIndex" Value="8000"/>
                                 <Trigger.EnterActions>
                                    <BeginStoryboard Storyboard="{StaticResource expandStoryboard}"/>
                                </Trigger.EnterActions>
                                <Trigger.ExitActions>
                                    <BeginStoryboard Storyboard="{StaticResource shrinkStoryboard}"/>
                                </Trigger.ExitActions>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ., Converter={StaticResource CanEnlargeConverter}}" Value="False">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>



        </Style>

    </UserControl.Resources>
    
    <StackPanel Orientation="Vertical" HorizontalAlignment="Stretch"  >


        <TextBlock
            Text="{x:Static props:Resources.Links_AND_Resources}" Panel.ZIndex="1"
            Style="{StaticResource ItemGroupTextBlock}" />

        <ItemsControl  x:Name="artifactListBox" ItemsSource="{Binding Artifacts}" Panel.ZIndex="2" HorizontalAlignment="Stretch">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid>

                        <Button Style="{StaticResource ArtifactImageButton}" Content="{Binding .}" Tag="{Binding ArtifactName}" />
                        <Button Style="{StaticResource ArtifactEnlargeImageButton}" Content="{Binding .}" Tag="{Binding ArtifactName}" />
                        
                        <StackPanel HorizontalAlignment="Right" VerticalAlignment="Top" Orientation="Horizontal" Panel.ZIndex="90000">

                            <Button                                                                                                                       
                                Style="{StaticResource EditActionButton}" Height="16" Width="16"                                           
                                Command="{Binding Model.StartEditCommand, ElementName=thisView}"
                                CommandParameter="{Binding ArtifactUId}"/>
                            <Button                                                                                       
                                Style="{StaticResource RemoveActionButton}" Height="16" Width="16"                                           
                                Command="{Binding Model.RemoveArtifactCommand, ElementName=thisView}"
                                CommandParameter="{Binding ArtifactUId}"/>
                            <StackPanel.Style>
                                <Style>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Path=IsMouseOver, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}" Value="False">
                                            <Setter Property="DockPanel.Visibility" Value="Hidden"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </StackPanel.Style>
                        </StackPanel>
                    </Grid>

                        

                    
                    
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Width="{Binding ActualWidth, ElementName=artifactListBox}" ClipToBounds="False" Panel.ZIndex="10" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>

        <Button Command="{Binding AddArtifactCommand}" Cursor="Hand" HorizontalAlignment="Stretch" Margin="0,0,0,5" Panel.ZIndex="1">
            <Border Style="{StaticResource MouseOverListBorder}" Padding="2" x:Name="item">
                <TextBlock HorizontalAlignment="Left">
                    <Image Source="/Images/Toolbar/Add.png" Width="16" Height="16" Stretch="Uniform" VerticalAlignment="Center" />    
                    <TextBlock Text="{x:Static props:Resources.Add_artifact}" Foreground="{StaticResource SubTextBrush}" VerticalAlignment="Center" />
                </TextBlock>
            </Border>
            <Button.Style>
                <Style TargetType="Button" BasedOn="{StaticResource CleanButton}">
                    <Style.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Visibility" Value="Collapsed"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>

    </StackPanel>
</UserControl>
